<template>
  <div class="icon-body">
    <el-input
      v-model="iconName"
      style="position: relative"
      clearable
      placeholder="请输入图标名称"
      @clear="filterIcons"
      @input="filterIcons"
    >
      <template #suffix>
        <Icon :class="'svg-icon:' + iconName" />
      </template>
    </el-input>
    <div class="items-list mt-4">
      <div
        class="mb--5 pr-6 cursor-pointer inline"
        v-for="(item, index) in iconList"
        :key="index"
        @click="selectedIcon(item)"
      >
        <Icon
          size="23"
          :title="item.split('icon-')[1]"
          :icon="'svg-icon:' + item.split('icon-')[1]"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts" name="IconSelect">
import icons from 'virtual:svg-icons-names'
import { ref } from 'vue'

const iconName = ref('')
const iconList = ref(icons)

const emit = defineEmits(['selected'])

function selectedIcon(name) {
  emit('selected', name.split('icon-')[1])
  document.body.click()
}

function filterIcons() {
  iconList.value = icons
  if (iconName.value) {
    iconList.value = icons.filter((item) => {
      return item.toLocaleLowerCase().indexOf(iconName.value.toLocaleLowerCase()) !== -1
    })
  }
}

function reset() {
  iconName.value = ''
  iconList.value = icons
}

defineExpose({
  reset,
})
</script>
<style lang="scss" scoped>
.icon-body {
  width: 100%;
  padding: 10px;

  .items-list {
    height: 140px;
    overflow-y: auto;
  }
}
</style>
